package com.bnyy.medicalHousekeeper.moudle.message.activity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;

import com.bnyy.medicalHousekeeper.DialogHelper;
import com.bnyy.medicalHousekeeper.R;
import com.bnyy.medicalHousekeeper.base.BaseActivityImpl;
import com.bnyy.medicalHousekeeper.moudle.message.MessageManager;
import com.bnyy.medicalHousekeeper.request.BaseObserverImpl;
import com.bnyy.medicalHousekeeper.request.RequestManager;
import com.bnyy.message.bean.ContactGroup;
import com.bnyy.message.bean.chat.Chat;
import com.bnyy.message.bean.chat.ChatMessage;
import com.bnyy.message.bean.chat.CreateGroupChatPermission;
import com.bnyy.message.enums.ChatMessageStatus;
import com.bnyy.message.enums.MessageType;
import com.kyleduo.switchbutton.SwitchButton;

import java.util.HashMap;

import butterknife.BindView;

/**
 * Author : 关炳鹏
 * Description :
 * Date : 2022/11/9
 **/
public class CreateGroupChatActivity extends BaseActivityImpl {
    @BindView(R.id.tv_count)
    TextView tvCount;
    @BindView(R.id.switch_button)
    SwitchButton switchButton;
    @BindView(R.id.tv_create)
    TextView tvCreate;
    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.tv_tips)
    TextView tvTips;

    @Override
    public int getLayoutId() {
        return R.layout.activity_message_create_group_chat;
    }

    @Override
    public String getTitleStr() {
        return " ";
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        etName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                int length = s.length();
                tvCount.setText(length + " /12 ");
                tvCreate.setEnabled(length > 0);
            }
        });

        tvCreate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogHelper.showNormalDialog(mContext, "确定创建群聊【" + etName.getText().toString() + "】吗?",
                        new DialogHelper.Callback() {
                            @Override
                            public void onRightClick(AlertDialog alertDialog) {
                                super.onRightClick(alertDialog);

                                HashMap<String, Object> params = new HashMap<>();
                                params.put("is_open", false);
                                params.put("group_name", etName.getText().toString());
                                requestManager.request(requestManager.mMessageRetrofitService.createGroupChat(RequestManager.getJsonRequestBody(params)),
                                        new BaseObserverImpl<ContactGroup>() {
                                            @Override
                                            public void onSuccess(ContactGroup contactGroup) {
                                                super.onSuccess(contactGroup);

                                                ChatMessage chatMessage = new ChatMessage(contactGroup, userInfoManager.getLoginUserInfo(), MessageType.GROUP_CHAT_CREATED);
                                                chatMessage.setRead(true);
                                                chatMessage.setGroupChatId(contactGroup.getId());
                                                chatMessage.setChatMessageStatus(ChatMessageStatus.SUCCESS);
                                                MessageManager.save(chatMessage);

                                                ChatMessageActivity.show(mContext, new Chat(userInfoManager.getLoginUserId(), chatMessage));
                                                mContext.finish();

                                            }
                                        });
                            }
                        });
            }
        });

        requestManager.request(requestManager.mMessageRetrofitService.createGroupChatPermission(),
                new BaseObserverImpl<CreateGroupChatPermission>() {
                    @Override
                    public void onSuccess(CreateGroupChatPermission createGroupChatPermission) {
                        super.onSuccess(createGroupChatPermission);

                        tvTips.setText(createGroupChatPermission.getContent());

                        if (!createGroupChatPermission.isAdd_group()) {
                            DialogHelper.showTipsDialog(mContext, createGroupChatPermission.getContent(),
                                    new View.OnClickListener() {
                                        @Override
                                        public void onClick(View v) {
                                            finish();
                                        }
                                    });
                        }
                    }
                });
    }
}
































